/*
 * VectorUtils.hpp
 *
 *  Created on: 24-11-2010
 *      Author: Przemek
 */

#pragma once

#include "pch.hpp"

/*
 * A namespace with global functions for manipulating STL vector class. It also contains a few functions
 * for operating on vector with boost::shared_ptr<> template argument.
 */
namespace VectorUtils
{
template<typename T>
static bool contains(const std::vector<T>& vector, const T value);

template<typename T>
static typename std::vector<T>::iterator find(std::vector<T>& vector, const T value);

template<typename T>
static typename std::vector<T>::const_iterator
find(const std::vector<T>& vector, const T value);

template<typename T>
static typename std::vector<boost::shared_ptr<T> >::iterator findInSharedPtrVector(std::vector<
		boost::shared_ptr<T> >& vector, const T* value);

template<typename T>
static typename std::vector<boost::shared_ptr<T> >::const_iterator findInSharedPtrVector(
		const std::vector<boost::shared_ptr<T> >& vector, const T* value);
}

template<typename T>
bool VectorUtils::contains(const std::vector<T>& vector, const T value)
{
	BOOST_FOREACH(T current, vector)
	{
		if(current == value)
		{
			return true;
		}
	}

	return false;
}

template<typename T>
typename std::vector<T>::iterator VectorUtils::find(std::vector<T>& vector, const T value)
{
	for(typename std::vector<T>::iterator iter = vector.begin(); iter != vector.end(); ++iter)
	{
		if((*iter) == value)
		{
			return iter;
		}
	}

	return vector.end();
}

template<typename T>
typename std::vector<T>::const_iterator VectorUtils::find(const std::vector<T>& vector,
		const T value)
{
	for(typename std::vector<T>::const_iterator iter = vector.begin(); iter != vector.end(); ++iter)
	{
		if((*iter) == value)
		{
			return iter;
		}
	}

	return vector.end();
}

template<typename T>
typename std::vector<boost::shared_ptr<T> >::iterator VectorUtils::findInSharedPtrVector(
		std::vector<boost::shared_ptr<T> >& vector, const T* value)
{
	for(typename std::vector<boost::shared_ptr<T> >::iterator iter = vector.begin(); iter
			!= vector.end(); ++iter)
	{
		if(iter->get() == value)
		{
			return iter;
		}
	}

	return vector.end();
}

template<typename T>
typename std::vector<boost::shared_ptr<T> >::const_iterator VectorUtils::findInSharedPtrVector(
		const std::vector<boost::shared_ptr<T> >& vector, const T* value)
{
	for(typename std::vector<boost::shared_ptr<T> >::const_iterator iter = vector.begin(); iter
			!= vector.end(); ++iter)
	{
		if(iter->get() == value)
		{
			return iter;
		}
	}

	return vector.end();
}

